//
//  HRCategoryViewController.m
//  haierapp
//
//  Created by zhang on 14-8-8.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRCategoryViewController.h"

#import "HRCategoryCell.h"

#import "HRGoodsListViewController.h"

#import "HRProCategoryList.h"



#define CELL_IDENTIFIER @"goodscategorycell"
#define CELL_HEIGHT 80.0f

@interface HRCategoryViewController ()

@property(nonatomic,strong) NSArray *dataArray;

@end

@implementation HRCategoryViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    if (_dataArray==nil || [_dataArray count]==0) {
        [self loadCategoryData];
    }
    
    UIView *footerView=[[UIView alloc]init];
    [footerView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setTableFooterView:footerView];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods
-(void)loadCategoryData{
    
    //隐藏无网络提示页面
    [self hideHud];
    
    //获取数据
    [self showCustomLoadingHud:kLoading inView:nil];
    [HRInterfaceManager getProCategoryList:^(BOOL isSucceed, NSString *message, id data) {
        [self hideCustomLoadingHud];
        if (isSucceed) {
            HRProCategoryList *categoryList=(HRProCategoryList *)data;
            
            self.dataArray=categoryList.list;
            
            [self.tableView reloadData];
            
        }else if([message isEqualToString:NO_WIFI_ERROR_CODE]){
            //显示无网络提示页面
            [self showHudByType:HUDTYPE_NO_WIFI];
        }else{
            [self showTextHUD:message];
        }
        
        //页面加载耗时
        [self analysisEnd:nil];
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = CELL_IDENTIFIER;
    HRCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    [cell configWithData:[_dataArray objectAtIndex:indexPath.row]];
    
    return cell;
}


#pragma mark - table view delegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  CELL_HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HRProCategory *proCategory=[_dataArray objectAtIndex:indexPath.row];
    
    HRGoodsListViewController *goodsListViewController=[[HRGoodsListViewController alloc]initWithNibName:NIBNAME_GOODSLIST bundle:nil];
    
    goodsListViewController.proCategoryId=[proCategory.classification1 stringValue];//产品类别
    goodsListViewController.type=TYPE_PRODUCT_LIST_BY_ID;//类别
    
    [self.tabBarController.navigationController pushViewController:goodsListViewController animated:YES];
    
}

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */


#pragma mark - HRPrograssHUD delegate Methods
//重新加载数据
-(void)tapHUDButtonAction:(id)button{
    [self loadCategoryData];
}

@end

